package nusweektracker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.Box;
import javax.swing.JSeparator;
import java.awt.Color;

public class SwingApplet extends JApplet implements ActionListener {
	private JTextField inputAcademicWeek;
	private JTextArea outputArea;
	private JTextField inputNbrDays;
	private Logic logic;
	private JRadioButton btnWeeks;
	private JTextField startDateInput;

	public void init() {
		resize(700, 400);
	}

	/**
	 * Create the applet.
	 */
	public SwingApplet() {

		logic = new Logic();
		getContentPane().setLayout(null);

		createFields();

		createLabels();

		createButtons();

	}

	/**
	 * Create text felds
	 */
	private void createFields() {
		createComponentInputAcademicWeek();

		createComponentInputNbrDays();

		createComponentOutputArea();

		createComponentStartDateInput();
	}

	/**
	 * 
	 */
	private void createComponentStartDateInput() {
		startDateInput = new JTextField();
		startDateInput.setText("none");
		startDateInput.setToolTipText("(Optional) Enter date to start counting from");
		startDateInput.setBounds(254, 131, 122, 27);
		startDateInput.addActionListener(this);
		getContentPane().add(startDateInput);
		startDateInput.setColumns(10);
	}

	/**
	 * 
	 */
	private void createButtons() {
		btnWeeks = new JRadioButton("Convert to weeks");
		btnWeeks.setToolTipText("Select this to convert days to weeks");
		btnWeeks.setBounds(32, 201, 182, 18);
		btnWeeks.addActionListener(this);
		getContentPane().add(btnWeeks);
	}

	/**
	 * 
	 */
	private void createLabels() {
		JLabel academicStartDateLabel = new JLabel("Enter date of 1st academic week");
		academicStartDateLabel.setBounds(48, 6, 212, 35);
		getContentPane().add(academicStartDateLabel);

		JLabel lblEnterNumberOf = new JLabel("Enter number of days to calculate");
		lblEnterNumberOf.setBounds(32, 106, 217, 15);
		getContentPane().add(lblEnterNumberOf);

		JLabel lblResult = new JLabel("Result");
		lblResult.setBounds(191, 164, 57, 15);
		getContentPane().add(lblResult);

		JLabel lblEnterStartDate = new JLabel("Enter start date (yyyy/mm/dd)");
		lblEnterStartDate.setBounds(261, 104, 192, 18);
		getContentPane().add(lblEnterStartDate);
	}

	/**
	 * 
	 */
	private void createComponentOutputArea() {
		outputArea = new JTextArea();
		outputArea.setEditable(false);
		outputArea.setToolTipText("Program output appears here");
		JScrollPane scrollPane = new JScrollPane(outputArea);
		scrollPane.setBounds(191, 194, 500, 200);
		getContentPane().add(scrollPane);
	}

	/**
	 * 
	 */
	private void createComponentInputNbrDays() {
		inputNbrDays = new JTextField();
		inputNbrDays.setToolTipText("enter nbr of days");
		inputNbrDays.setBounds(48, 142, 80, 16);
		inputNbrDays.addActionListener(this);
		getContentPane().add(inputNbrDays);
	}

	/**
	 * 
	 */
	private void createComponentInputAcademicWeek() {
		inputAcademicWeek = new JTextField();
		inputAcademicWeek.setToolTipText("YYYY/MM/DD");
		inputAcademicWeek.setText("2013/8/12");
		inputAcademicWeek.setBounds(48, 45, 134, 28);
		getContentPane().add(inputAcademicWeek);
		inputAcademicWeek.setColumns(10);
		inputAcademicWeek.addActionListener(this);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == inputAcademicWeek) {
			calculateAcademicWeek();
		} else if (evt.getSource() == inputNbrDays) {
			calculateNbrDays();
		} else if (evt.getSource() == btnWeeks) {
			convertDaysToWeeks();
		} else if (evt.getSource() == startDateInput) {
			calculateNbrDays();
		}

	}

	/**
	 * 
	 */
	private void convertDaysToWeeks() {
		try {
			int days = Integer.parseInt(inputNbrDays.getText());
			days = days * 7;
			
			inputNbrDays.setText(String.valueOf(days));
			calculateNbrDays();

		} catch (NumberFormatException ex) {
			print("NumberFormatException for inputNbrDays!!");
		}
	}

	/**
	 * Gives current academic week.
	 */
	private void calculateAcademicWeek() {
		try {
			String[] toks = inputAcademicWeek.getText().split("/");
			int year = Integer.parseInt(toks[0]);
			int month = Integer.parseInt(toks[1]);
			int date = Integer.parseInt(toks[2]);
			logic.calculateCurrentWeek(year, month, date);
			printLogicOutput();
		} catch (NumberFormatException ex) {
			print("Exception - numberFormat");
		}
	}

	/**
	 * Calculates number of days from inputNbrDays If startDateInput is none, take
	 * today as start date.
	 */
	private void calculateNbrDays() {
		try {
			if (startDateInput.getText().equals("none")) {
				logic.addDays(Integer.parseInt(inputNbrDays.getText()));
			} else {
				calculateNbrDaysWithStartDate();
			}
			printLogicOutput();
		} catch (NumberFormatException ex) {
			print("Exception - numberFormat");
		}
	}

	/**
	 * Calculates number of days from startDateInput. Days specified in
	 * inputNbrDays.
	 */
	private void calculateNbrDaysWithStartDate() {
		String[] toks = startDateInput.getText().split("/");
		int year = Integer.parseInt(toks[0]);
		int month = Integer.parseInt(toks[1]);
		int date = Integer.parseInt(toks[2]);
		logic.addDays(year, month, date, Integer.parseInt(inputNbrDays.getText()));
	}

	private void printLogicOutput() {
		outputArea.setText("");
		while (!logic.getOutputs().isEmpty()) {
			print(logic.getOutputs().poll());
		}

	}

	public void print(String text) {
		outputArea.append(text + "\n");
	}
}
